﻿/**
 * 
 * Copyright 2009 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace mooseirc.utility {

    /// <summary>
    /// Provides Utility Network/Socket Functionality
    /// </summary>
    [CLSCompliant(true)]
    public class NetworkUtility {

        /// <summary>
        /// Just Create and Return a Socket, configured for the
        /// usual Tcp Scenario
        /// </summary>
        /// <returns>New Socket</returns>
        public static Socket createTcpSocket() {
            return new Socket(AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);
        }

        /// <summary>
        /// Resolve Host into IP Address
        /// </summary>
        /// <param name="host">host/ip address in literal form</param>
        /// <returns>IP address, null if cannot be looked up</returns>
        public static IPAddress resolve(string host) {
            IPHostEntry entry = null;
            try {
                entry = Dns.GetHostEntry(host);
            } catch (Exception) {
                return null;
            }
            foreach (IPAddress address in entry.AddressList) {
                if (!address.IsIPv6LinkLocal && !address.IsIPv6Multicast && !address.IsIPv6SiteLocal) {
                    return address;
                }
            }
            return null;
        }

    }
}
